from base import base_schemas
from typing import Optional, List
from pydantic import UUID4, EmailStr, Field, field_validator


class MenuBaseSchema(base_schemas.BaseModel):
    title: str
    icon: Optional[str] = None
    path: Optional[str] = None
    is_external: bool = False
    sort: int = 0


class MenuEditSchema(MenuBaseSchema, base_schemas.BaseEditSchema):
    parent_id: Optional[UUID4] = None


class MenuOutSchema(MenuBaseSchema, base_schemas.BaseOutSchema):
    parent_id: Optional[UUID4] = None
    children: Optional[List["MenuOutSchema"]] = None


class GlobalConfigBaseSchema(base_schemas.BaseModel):
    title: str
    description: Optional[str] = None
    keywords: Optional[str] = None
    logo_text: str
    icp: Optional[str] = None
    sider_name: str
    sider_avatar: str
    sider_description: str
    sider_cover: str
    github_url: Optional[str] = None
    email: Optional[EmailStr] = None
    tag_cloud: Optional[bool] = True
    time_line: Optional[bool] = True
    recent_posts: Optional[bool] = True
    index_title: Optional[str] = None
    index_subtitle: Optional[str] = None
    index_cover: Optional[str] = None
    index_blurhash: Optional[str] = None
    category_cover: Optional[str] = None
    category_blurhash: Optional[str] = None
    tag_cover: Optional[str] = None
    tag_blurhash: Optional[str] = None
    archive_cover: Optional[str] = None
    archive_blurhash: Optional[str] = None
    message_cover: Optional[str] = None
    message_blurhash: Optional[str] = None
    thought_cover: Optional[str] = None
    thought_blurhash: Optional[str] = None

    @field_validator("email")
    @classmethod
    def validate_email(cls, v):
        if v is None:
            return v
        try:
            EmailStr.validate(v)
            return v
        except ValueError:
            raise ValueError("请输入有效的邮箱地址")


class GlobalConfigEditSchema(GlobalConfigBaseSchema):
    id: Optional[UUID4] = None


class GlobalConfigOutSchema(GlobalConfigBaseSchema, base_schemas.BaseOutSchema):
    pass


class VisitorOutSchema(base_schemas.BaseOutSchema):
    ip: Optional[str] = None
    country: Optional[str] = None
    region: Optional[str] = None
    city: Optional[str] = None
    count: int


class VisitorFilterSchema(base_schemas.BaseFilterSchema):
    ip: Optional[str] = None
    country: Optional[str] = None
    region: Optional[str] = None
    city: Optional[str] = None


MenuOutSchema.model_rebuild()
